package com.crayfish.quduo.utils;

import com.crayfish.quduo.model.UserBean;

import java.lang.reflect.ParameterizedType;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by crayfish on 2017/1/23.
 */
public class BmobManager<T> {

    public static BmobManager instance;

    public static BmobManager getInstance(){
        if(instance == null){
            instance = new BmobManager();
        }
        return instance;
    }
    public void SQLQuery(String sql,SQLQueryListener<T> listener){
        BmobQuery<T> query = new BmobQuery<>();
        query.setSQL(sql);
        boolean isCache = query.hasCachedResult((Class<T>)((ParameterizedType)getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0]);
        if(isCache){
            query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ELSE_NETWORK);	// 先从缓存取数据，如果没有的话，再从网络取。
        }else{
            query.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE);	// 如果没有缓存的话，则先从网络中取
        }
        query.doSQLQuery(sql,listener);
    }
}
